﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Communergy.Common;
using Communergy.Repository.SQLite;

namespace Communergy.Repository
{
    public class SQLiteHistoricRepository : IHistoricRepository
    {
		#region Fields (1) 

        CommunergySQLiteEntities sqlite;
        

		#endregion Fields 

		#region Constructors (1) 

        public SQLiteHistoricRepository(IUnitOfWork unitOfWork)
        {
            //will return null if not communergyEntities
            sqlite = unitOfWork as CommunergySQLiteEntities;
            if (sqlite == null)
                throw new ArgumentOutOfRangeException("unitOfWork");           
        }

        public SQLiteHistoricRepository(string connectionString)
        {
            sqlite = new CommunergySQLiteEntities(connectionString);            
        }

		#endregion Constructors 



        #region IHistoricRepository Members

        /// <summary>
        /// Insert_s the electricity hourly.
        /// </summary>
        /// <param Name="meterId">The meter id.</param>
        /// <param Name="hourlyEnergyReadings">The hourly energy readings.</param>
        public void Insert_ElectricityHourly(string meterId, List<HistoricalEnergyReading> hourlyEnergyReadings)
        {
            //using (var sqlite = new CommunergySQLiteEntities(connString))
            {
                long meterIdLong = Convert.ToInt32(meterId);
                var meter = sqlite.ElectricityMeter.Include("ElectricityHourly_Auto").Where(em => em.id == meterIdLong).FirstOrDefault();
                if (meter != null)
                {
                    foreach (var reading in hourlyEnergyReadings)
                    {
                        var dbReading = meter.ElectricityHourly_Auto.Where(r => r.hour == reading.ReadingDateTime).FirstOrDefault();

                        if (dbReading == null)
                        {
                            var sqliteReading = new ElectricityHourly_Auto
                            {
                                hour = reading.ReadingDateTime,
                                kWh = (double)reading.KWh

                            };
                            meter.ElectricityHourly_Auto.Add(sqliteReading);
                        }
                    }
                }
                //sqlite.SaveChanges();
            }
        }

        /// <summary>
        /// Inserts the electricity daily.
        /// </summary>
        /// <param Name="meterId">The meter id.</param>
        /// <param Name="dailyEnergyReadings">The daily energy readings.</param>
        public void Insert_ElectricityDaily(string meterId, List<HistoricalEnergyReading> dailyEnergyReadings)
        {
            //using (var sqlite = new CommunergySQLiteEntities(connString))
            {
                long meterIdLong = Convert.ToInt32(meterId);
                var meter = sqlite.ElectricityMeter.Include("ElectricityDaily_Auto").Where(em => em.id == meterIdLong).FirstOrDefault();
                if (meter != null)
                {
                    foreach (var reading in dailyEnergyReadings)
                    {
                        var dbReading = meter.ElectricityDaily_Auto.Where(r => r.day.ToString() == reading.ReadingDateTime.Date.ToString()).FirstOrDefault();
                        if (dbReading == null)
                        {
                            var sqliteReading = new ElectricityDaily_Auto
                            {
                                day = reading.ReadingDateTime.Date,
                                kWh = (double)reading.KWh

                            };
                            meter.ElectricityDaily_Auto.Add(sqliteReading);
                        }
                    }
                }
                //sqlite.SaveChanges();
            }
        }

        public void Insert_ElectricityMonthly(string meterId, List<HistoricalEnergyReading> monthlyEnergyReadings)
        {
            //using (var sqlite = new CommunergySQLiteEntities(connString))
            {
                long meterIdLong = Convert.ToInt32(meterId);
                var meter = sqlite.ElectricityMeter.Include("ElectricityMonthly_Auto").Where(em => em.id == meterIdLong).FirstOrDefault();
                if (meter != null)
                {
                    foreach (var reading in monthlyEnergyReadings)
                    {
                        var dbReading = meter.ElectricityMonthly_Auto.Where(r => r.month.ToString() == reading.ReadingDateTime.Date.ToString()).FirstOrDefault();
                        if (dbReading == null)
                        {
                            var sqliteReading = new ElectricityMonthly_Auto
                            {
                                month = reading.ReadingDateTime.Date,
                                kWh = (double)reading.KWh

                            };
                            meter.ElectricityMonthly_Auto.Add(sqliteReading);
                        }
                    }
                }
                //sqlite.SaveChanges();
            }
        }

        public List<HistoricalEnergyReading> GetHourly(string meterId, DateTime dateFrom)
        {
            //using (var sqlite = new CommunergySQLiteEntities(connString))
            {
                long meterIdLong = Convert.ToInt32(meterId);
                var meter = sqlite.ElectricityMeter.Include("ElectricityHourly_Auto").Where(em => em.id == meterIdLong).FirstOrDefault();
                if (meter != null)
                {
                    var dateTo = dateFrom.AddDays(1);
                    var hourlyData = meter.ElectricityHourly_Auto.Where(h => h.hour >= dateFrom.Date && h.hour < dateTo).OrderBy(h => h.hour).ToList();
                    var hourly = (from h in hourlyData
                                  select new HistoricalEnergyReading
                                  {
                                      ReadingDateTime = h.hour,
                                      KWh = (decimal)h.kWh
                                  }).ToList();
                    return hourly;
                }

                else
                    return null;
            }
        }

        #endregion
    }
}
